<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-row>
                <el-col :span="7">
                    <!-- 搜索框 -->
                    <el-input
                        placeholder="请输入内容"
                        v-model="queryInfo.query"
                        clearable @clear="getGoodList"
                    >
                        <el-button slot="append" icon="el-icon-search" @click="getGoodList">
                        </el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button class="add_btn" type="primary" @click="goAddpage">添加商品</el-button>
                </el-col>
            </el-row>
            <el-table
                :data="goodList"
                stripe border
                style="width: 100%">
                <el-table-column
                type="index"
                label="#"
                width="50"
                >
                </el-table-column>
                <el-table-column
                prop="goods_name"
                label="商品名称"
                >
                </el-table-column>
                <el-table-column
                prop="goods_price"
                label="商品价格(元)"
                width="120">
                </el-table-column>
                <el-table-column
                prop="goods_number"
                label="商品数量" width="80">
                </el-table-column>
                <el-table-column
                prop="add_time"
                label="创建时间" width="200">
                <template slot-scope="scope">
                    {{scope.row.add_time | dateFormat}}
                </template>
                </el-table-column>
                <el-table-column
                prop="methods"
                label="操作" width="140">
                <template slot-scope="scope">
                    <el-button type="primary" size="mini" icon="el-icon-edit" @click="showEditDialog(scope.row.goods_id)"></el-button>
                    <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteGood(scope.row)"></el-button>
                </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[5, 20, 50, 100]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </el-card>
        <el-dialog
            title="修改商品信息"
            :visible.sync="editGoodssDialogVisiable"
            width="30%"
            >
            <el-form :model="editGoodssForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
                <el-form-item label="商品名称" prop="goods_name">
                    <el-input v-model="editGoodssForm.goods_name"></el-input>
                </el-form-item>
                <el-form-item label="价格" prop="goods_price">
                    <el-input v-model="editGoodssForm.goods_price"></el-input>
                </el-form-item>
                <el-form-item label="数量" prop="goods_number">
                    <el-input v-model="editGoodssForm.goods_number"></el-input>
                </el-form-item>
                <el-form-item label="重量" prop="goods_weight">
                    <el-input v-model="editGoodssForm.goods_weight"></el-input>
                </el-form-item>
                <el-form-item label="商品介绍" prop="goods_introduce">
                    <el-input type="textarea" :rows="3"  v-model="editGoodssForm.goods_introduce"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                    <el-button @click="editGoodssDialogVisiable = false">取 消</el-button>
                <el-button type="primary" @click="editGoodsInfo">确 定</el-button>
                
            </span>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data() {
        return{
            goodList:[],
            queryInfo:{
                query:'',
                pagenum:2,
                pagesize:5,
            },
            
            total:0,
            addGoodsVisiable:false,
            editGoodssForm:{},
            editGoodssDialogVisiable:false,
            editFormRules:{
                goods_name:[{ required: true, message: '请输入商品名称', trigger: 'blur' }],
                goods_price:[{ required: true, message: '请输入商品价格', trigger: 'blur' }],
                goods_number:[{ required: true, message: '请输入商品数量', trigger: 'blur' }],
                goods_weight:[{ required: true, message: '请输入商品种类', trigger: 'blur' }],
                goods_introduce:[{ required: true, message: '请输入介绍', trigger: 'blur' }],
            }
            
        }
    },
    mounted(){
        this.getGoodList()
    },
    methods:{
        // 获取商品列表
        async getGoodList(){
            const {data:res} = await this.$http.get('goods',{params:this.queryInfo})
            if(res.meta.status !== 200) return this.$message.error('获取数据失败')
            this.goodList = res.data.goods
            this.total = res.data.total
            // this.goodList[add_time] = this.getLocalTime(add_time)
        },
        getLocalTime(nS) {  
            return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');  
        },
        // 改变页码
        handleSizeChange(newSize){
            this.queryInfo.pagesize = newSize
            this.getGoodList()
        },
        // 
        handleCurrentChange(newNum){
            this.queryInfo.pagenum = newNum
            this.getGoodList() 
        },
        // 打开添加对话框
        showAddDialog(){
            this.addGoodsVisiable = true
        },
        // 打开编辑对话框
        async showEditDialog(goods_id){
           this.editGoodssDialogVisiable = true
            const {data:res} = await this.$http.get('goods/'+goods_id)
            this.editGoodssForm = res.data
            console.log(this.editGoodssForm);
        },
        editGoodsInfo(){
            this.$refs.editFormRef.validate(async valid=>{
                if(!valid) return
                const { data: res } = await this.$http.put('goods/'+this.editGoodssForm.goods_id,{
                    goods_name:this.editGoodssForm.goods_name,
                    goods_price:this.editGoodssForm.goods_price,
                    goods_number:this.editGoodssForm.goods_number,
                    goods_weight:this.editGoodssForm.goods_weight,
                    goods_introduce:this.editGoodssForm.goods_introduce
                })
                if(res.meta.status !== 400) return this.$message.error('修改商品信息失败')
                this.$message.success('修改商品信息成功')
                this.getGoodList()
                this.editGoodssDialogVisiable = false
            })
        },
        // 删除列表内容
        async deleteGood(row){
            const configResult = await this.$confirm(
                '此操作将永久删除该商品, 是否继续?',
                '提示',
                {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                }
            )
            if(configResult !== 'confirm') return this.$message.info('您已取消操作')
            const {data:res} = await this.$http.delete('goods/'+row.goods_id)
            console.log(res);
            if(res.meta.status !== 200) return this.$message.error('删除数据失败')
            this.$message.success('删除数据成功')
            this.getGoodList()
        },
        // 跳转至添加商品页面
        goAddpage(){
            this.$router.push('/goods/add')
        }
    }
}
</script>
<style lang="less" scoped>
.add_btn {
    margin-left: 10px;
}
.el-col{
    margin:10px 0
}
</style>